/**
 * <strong>Title : WarehouseController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.mm.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2016年3月1日 下午1:44:12<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.mm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.StorageLocation;
import cn.b.sky.mes.mm.model.Warehouse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.StorageLocationType;
import cn.b.sky.mes.mm.model.condition.WarehouseCondition;
import cn.b.sky.mes.mm.service.StorageLocationService;
import cn.b.sky.mes.mm.service.WarehouseService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : WarehouseController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.mm.controller<br>
 * </strong> <strong>Description : </strong>仓库主数据<br>
 * <strong>Create on : 2016年3月1日 下午1:44:12<br>
 * </strong>
 * <p>
 * 
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/warehouse/*")
public class WarehouseController extends SecurityController {

	@Resource
	WarehouseService warehouseService;
	@Resource
	StorageLocationService storageLocationService;

	@RequestMapping("warehouseList")
	public String selectDefectTypeListByCondition(String navTabId, Model model) {
		model.addAttribute("navTabId", navTabId);
		return "mm/warehouse/warehouseList";
	}

	@RequestMapping("selectWarehouseList")
	@ResponseBody
	public DWZResult selectDefectTypeList(
			@ModelAttribute("condition") WarehouseCondition condition) {
		condition.setSiteCode(getSiteCode());
		List<Warehouse> list = warehouseService
				.selectWarehouseListByCondition(condition);
		int count = warehouseService.selectWarehouseCountByCondition(condition);
		condition.setTotalCount(count);
		condition.setData(list);
		return ajaxDone(condition);
	}

	@RequestMapping("addWarehouse")
	public String addBom(Model model, String navTabId) {
		model.addAttribute("navTabId", navTabId);
		return "mm/warehouse/warehouse_add";
	}

	@RequestMapping("editWarehouse")
	public String editWarehouse(Model model, String navTabId, String id) {
		Warehouse warehouse = warehouseService.getWarehouse(id, getSiteCode());
		model.addAttribute("warehouse", warehouse);
		model.addAttribute("navTabId", navTabId);
		return "mm/warehouse/warehouse_add";
	}

	@RequestMapping("viewWarehouse")
	public String viewWarehouse(Model model, String navTabId, String id) {
		Warehouse warehouse = warehouseService.getWarehouse(id, getSiteCode());
		List<StorageLocationType> storageLocationTypeList = warehouseService
				.selectStorageLocationListByWarehouseCode(id, getSiteCode());
		model.addAttribute("warehouse", warehouse);
		model.addAttribute("navTabId", navTabId);
		model.addAttribute("pattern", "view");
		model.addAttribute("storageLocationTypeList", storageLocationTypeList);
		return "mm/warehouse/warehouse_view";
	}

	@RequestMapping("saveWarehouse")
	@ResponseBody
	public DWZResult saveWarehouse(Model model, String navTabId, String id,
			Warehouse warehouse) {
		if (warehouse == null) {
			return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
		}
		if (warehouse.getLockFlag() == null) {
			warehouse.setLockFlag(0);
		}
		if (warehouse.getLocationManageFlag() == null) {
			warehouse.setLocationManageFlag(0);
		}
		if (warehouse.getNegativeInventoryFlag() == null) {
			warehouse.setNegativeInventoryFlag(0);
		}
		setCreateInfo(warehouse);
		warehouse.setSiteCode(getSiteCode());
		if (StringUtils.isBlank(id)) {
			warehouseService.insertWarehouse(warehouse);
		} else {
			warehouseService.updateWarehouse(warehouse);
		}
		return dialogAjaxDone(
				getImeViewMessageI18n(null, "successfulOperation.M"), navTabId,
				null, null, null);
	}

	@RequestMapping("deleteWarehouse")
	@ResponseBody
	public DWZResult deleteWarehouse(Model model, String navTabId,
			HttpServletRequest request) {

		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
		}
		String[] codes = ids.split(",");
		int len = codes.length;
		BatchJobReport[] reports = new BatchJobReport[len];
		boolean success = true;
		for (int i = 0; i < len; i++) {
			reports[i] = warehouseService.deleteWarehouse(codes[i],
					getSiteCode());
			if (!reports[i].isSuccess()) {
				success = false;
			}
		}
		if (success) {
			return navTabAjaxDone(navTabId, null, null, null,
					getImeViewMessageI18n(null, "successfulOperation.M"), null);
		} else {
			return batchjobAjaxDone(request, Arrays.asList(reports));
		}
	}

	@RequestMapping("warehouseLookUp")
	public String warehouseLookUp(Model model, String lookupType,
			@ModelAttribute("condition") WarehouseCondition condition,
			String type) {
		condition.setSiteCode(getSiteCode());
		if (!"all".equals(type)) {
			condition.setLockFlag(0);
		}
		List<Warehouse> list = warehouseService
				.selectWarehouseListByCondition(condition);
		int count = warehouseService.selectWarehouseCountByCondition(condition);
		condition.setData(list);
		condition.setTotalCount(count);
		model.addAttribute("page", condition);
		return "mm/warehouse/warehouse_lookup";
	}

	@RequestMapping("warehouseSuggest")
	@ResponseBody
	public List<Warehouse> warehouseSuggest(
			@ModelAttribute("condition") WarehouseCondition condition,
			String fromWarehouseCode, String toWarehouseCode, String type) {
		condition.setSiteCode(this.getSiteCode());
		if (StringUtils.isBlank(condition.getWarehouseCode())) {
			if (!StringUtils.isBlank(toWarehouseCode)) {
				condition.setWarehouseCode(toWarehouseCode);
			} else {
				condition.setWarehouseCode(fromWarehouseCode);
			}
		}
		if (!"all".equals(type)) {
			condition.setLockFlag(0);
		}
		return warehouseService.selectWarehouseListSuggests(condition);
	}

	@RequestMapping(value = "validateWarehouseCode")
	@ResponseBody
	public boolean validateWarehouseCode(String warehouseCode) {
		if (!StringUtils.isEmpty(warehouseCode)) {
			Warehouse warehouse = warehouseService.getWarehouse(warehouseCode,
					getSiteCode());
			return warehouse != null && warehouse.getLockFlag() != 1;
		}
		return false;
	}

	@RequestMapping(value = "checkWarehouseCode")
	@ResponseBody
	public boolean checkWarehouseCodeNotExists(String warehouseCode) {
		if (!StringUtils.isEmpty(warehouseCode)) {
			Warehouse warehouse = warehouseService.getWarehouse(warehouseCode,
					getSiteCode());
			return warehouse == null;
		}
		return true;
	}

	@RequestMapping(value = "selectWarehouse")
	@ResponseBody
	public DWZResult selectWarehouse() {
		return ajaxDone(warehouseService
				.selectUnLockWarehouseListBySiteCode(getSiteCode()));

	}

	@RequestMapping(value = "warehouseChange")
	@ResponseBody
	public DWZResult warehouseChange(Model model, String navTabId,
			String warehouseCode) {
		Warehouse warehouse = warehouseService.getWarehouse(warehouseCode,
				getSiteCode());
		List<StorageLocation> locations = null;
		if (warehouse != null
				&& warehouse.getLocationManageFlag().intValue() == 1) {
			locations = storageLocationService
					.selectStorageLocationByWarehouse(getSiteCode(),
							warehouseCode);
		}
		return ajaxDone(locations);
	}
}
